package com.ethan.digui;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ethan
 * create 2018-07-01 下午10:08
 * desc
 **/
public class Test {
    public static void main(String[] args){
       List<Category> list =  getList2(0);
       String a="";
    }

    public static List<Category> getList(int id){
        List<Category> list = Lists.newArrayList();
        List<Category> childList= getDaoList(id);

        if(!childList.isEmpty()){
            list =allList(list,childList);

            for (Category item:childList) {
                list =allList(list,getList(item.getId()));
            }
        }
        return list;
    }

    public static List<Category> getList2(int id){
        List<Category> list = Lists.newArrayList();
        List<Category> childList= getDaoList(id);
        if(!childList.isEmpty()){
            for (Category item:childList) {
                Category category2= Category.builder()
                        .id(item.getId())
                        .pid(item.getPid())
                        .name(item.getName())
                        .categoryList(getList2(item.getId()))
                        .build();
                list.add(category2);
            }
        }
        return list;
    }

    public static List<Category> allList(List<Category> list, List<Category> childList){
        if(list != null && !childList.isEmpty()){
            list.addAll(childList);
        }
        return list;
    }

    public static List<Category> getDaoList(int id){
        // id  pid name
        // 1    0  中国
        // 2    1  北京市
        // 3    1  湖北省
        // 4    2  朝阳区
        // 5    2  海淀区
        // 6    3  武汉市
        // 7    6  汉口


        Category category1 = new Category(1,0,"中国",null);
        Category category2 = new Category(2,1,"北京市",null);
        Category category3 = new Category(3,1,"湖北省",null);

        Category category4 = new Category(4,2,"朝阳区",null);
        Category category5 = new Category(5,2,"海淀区",null);

        Category category6 = new Category(6,3,"武汉市",null);
        Category category7 = new Category(7,6,"汉口",null);
        Category category8 = new Category(8,7,"硚口区",null);

        List<Category> initList = Lists.newArrayList(category1,category2,category3,category4,category5,category6,category7,category8);
        return initList.stream().filter(m-> id == m.getPid() ).collect(Collectors.toList());
    }

}
